﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Documents
//  File name:	ListFormsService.cs
//  Developer:	Solomatov Igor
//  Created:	16.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using Smart.Common;
using Smart.Common.Entities;
using Smart.Common.Forms;
//using Smart.Documents.Scopes;

namespace Smart.Documents.Services
{
	/// <summary>
	/// SharePoint list item forms service
	/// </summary>
	public class ListFormsService : WebFormsService
	{
		/// <summary>
		/// Create forms source
		/// </summary>
		public override FormsSource CreateSource()
		{
			return new ListFormsSource(Ctx);
		}

		/// <summary>
		/// Create forms formatter
		/// </summary>
		public override FormsFormatter CreateFormatter()
		{
			return new ListFormsFormatter(Ctx);
		}

		/// <summary>
		/// Create forms builder
		/// </summary>
		public override FormsBuilder CreateBuilder()
		{
			return new ListFormsBuilder(Ctx);
		}

		public override bool SetFieldValue(string fieldName, string fieldValue)
		{
			var ff = GetFormField(fieldName);

			if (ff != null)
			{
				if (fieldValue.IsNullOrEmpty())
				{
					ff.Value = null;
					return true;
				}

				/*				switch (fieldValue.ToLower())
								{
									case "[currentuserid]":
										ff.Value = SPH.FormatLookupValue(SPH.GetCurrentUser().ID, SPH.GetCurrentUser().Name);
										return true;

									case "[currentusermanagerid]":
										using (var os = SPScopes.OrgStructure)
										{
											var employee = os.GetEmployee(SPH.GetCurrentUser());
											var manager = os.GetEmployeeManager(employee);

											if (manager != null && !manager.User.IsEmpty)
												ff.Value = SPH.FormatLookupValue(manager.User.ID.Int, manager.User.Title);
										}
										return true;

									case "[currentuserdepartmentmanagerid]":
										using (var os = SPScopes.OrgStructure)
										{
											var employee = os.GetEmployee(SPH.GetCurrentUser());
											var department = os.GetEmployeeDepartment(employee);
											var manager = os.GetDepartmentManager(department);

											if (manager != null && !manager.User.IsEmpty)
												ff.Value = SPH.FormatLookupValue(manager.User.ID.Int, manager.User.Title);
										}
										return true;

									case "[currentusertopdepartmentmanagerid]":
										using (var os = SPScopes.OrgStructure)
										{
											var employee = os.GetEmployee(SPH.GetCurrentUser());
											var department = os.GetEmployeeDepartment(employee);
											var top = os.GetTopDepartment(department);
											var manager = os.GetDepartmentManager(top);

											if (manager != null && !manager.User.IsEmpty)
												ff.Value = SPH.FormatLookupValue(manager.User.ID.Int, manager.User.Title);
										}
										return true;
								}*/

				ff.Value = fieldValue;
				return true;
			}
			else
			{
				var webEntity = Ctx.Entity.Source as WebEntity;

				if (webEntity == null)
					return false;

				var item = webEntity.GetItem();

				if (fieldValue.IsNullOrEmpty())
				{
					item.Set(fieldName, null);

					return true;
				}

				switch (fieldValue.ToLower())
				{
					case "[currentuserid]":
						item.Set(fieldName, SPH.GetCurrentUser().ID);
						return true;
				}

				item.Set(fieldName, fieldValue);
				return true;
			}
		}

		/*
		protected override void OnFormChoosed()
		{
			AddAssignmentsButtons();

			base.OnFormChoosed();
		}

		protected virtual void AddAssignmentsButtons()
		{
			if (Ctx.Mode == MetaFormMode.New)
				return;

			if (!WebContext.IsScopeAccessible(Const.ScopeNames.Assignments))
				return;

			using (var assScope = SPScopes.Assignments)
			using (var docScope = SPScopes.Documents)
			{
				var builder = (ListFormsBuilder)Builder;

				var assignments = assScope.GetMyAssignmentsForItem(Ctx.Meta.ID, Ctx.Entity.ID.Int);

				if (assignments == null || assignments.Count <= 0)
					return;

				var tabs = Ctx.Form.ButtonTabs;

				//var aweb = assScope.Web;
				var ascopemeta = (MetaWebScope)assScope.Meta;
				var ameta = assScope.Assignments.Service.Meta;

				var esfield = ameta.Field("AExecutionStatus");
				var adfield = ameta.Field("ADecision");
				var tsfield = ameta.Field(SN.StatusField);

				var escompleted = AssignmentsContext.MapExecutionStatusID(ExecutionStatus.Completed).ToString();
				var tscompleted = SPUtility.GetLocalizedString("$Resources:core,Tasks_Completed;", "Core", Documents.GetLang());

				var initial = (tabs == null || tabs.Items == null || tabs.Items.Count == 0);

				foreach (var assignment in assignments.OfType<AssignmentApprove>())
				{
					if (tabs == null)
					{
						Ctx.Form.ButtonTabs = tabs = builder.CreateButtonTabs();

						tabs.Name = "tabs";

						Ctx.Form.SetParent();
					}

					var tab = builder.CreateButtonTab("AssignmentApproveTabTitle".Localized(), "aat" + assignment.ID, initial);
					tabs.Items.Add(tab);

					tabs.SetParent();

					initial = false;

					var group = builder.CreateButtonGroup(assignment.Title, tab.Name + "_aag" + assignment.ID, ButtonGroupTemplate.SimpleTemplate);
					tab.Items.Add(group);

					tab.SetParent();

					var decisions =
						docScope.ApprovalDecisions.Query
							.Where.In("BSStatusId",
									  (int)ApprovalDecision.Approved,
									  (int)ApprovalDecision.ApprovedWithComments,
									  (int)ApprovalDecision.Rejected)
							.Executor.Execute();

					foreach (var decision in decisions)
					{
						var url =
							SPH.GetDirectEditFormUrl(ascopemeta, ameta.ID, assignment.ID.Int, assignment.ContentTypeId.ToString(), true) +
							SPH.GetUrlActionSet(adfield.Title, adfield.Type, decision.ID.ToString(), false) +
							SPH.GetUrlActionSet(tsfield.Title, tsfield.Type, tscompleted, true) +
							SPH.GetUrlActionSet(esfield.Title, esfield.Type, escompleted, true);

						int imageX;
						int imageY;

						AssignmentsContext.GetApprovalDecisionImage(decision, out imageX, out imageY);

						var button = builder.CreateButton(decision.Title, group.Name + "_aab" + decision.ID,
														  string.Format("ShowPopupDialog('{0}')", url));

						button.UseStandardImage = true;
						button.ImageLang = Formatter.GetCurrentLCID();
						button.ImageX = imageX;
						button.ImageY = imageY;

						group.Items.Add(button);
					}

					group.SetParent();
				}

				foreach (var assignment in assignments.OfType<AssignmentRead>())
				{
					if (tabs == null)
					{
						Ctx.Form.ButtonTabs = tabs = builder.CreateButtonTabs();

						tabs.Name = "tabs";

						Ctx.Form.SetParent();
					}

					var tab = builder.CreateButtonTab("AssignmentReadTabTitle".Localized(), "art" + assignment.ID, initial);
					tabs.Items.Add(tab);

					tabs.SetParent();

					initial = false;

					var group = builder.CreateButtonGroup(assignment.Title, tab.Name + "_arg" + assignment.ID, ButtonGroupTemplate.SimpleTemplate);
					tab.Items.Add(group);

					tab.SetParent();

					var url =
						SPH.GetDirectEditFormUrl(ascopemeta, ameta.ID, assignment.ID.Int, assignment.ContentTypeId.ToString(), true) +
						SPH.GetUrlActionSet(tsfield.Title, tsfield.Type, tscompleted, true) +
						SPH.GetUrlActionSet(esfield.Title, esfield.Type, escompleted, true);

					int imageX;
					int imageY;

					AssignmentsContext.GetConfirmReadImage(out imageX, out imageY);

					var button = builder.CreateButton(assignment.Title, group.Name + "_arb" + assignment.ID,
													  string.Format("ShowPopupDialog('{0}')", url));

					button.UseStandardImage = true;
					button.ImageLang = Formatter.GetCurrentLCID();
					button.ImageX = imageX;
					button.ImageY = imageY;

					group.Items.Add(button);

					group.SetParent();
				}
			}
		}
		 */
	}
}